import Vue from 'vue'
import Vuex from 'vuex'
import actions from './action'
import getters from './getters'
import state from './state'
import mutations from './mutations'
import createVuexAlong from 'vuex-along'
import { store as anovStore } from 'anov-core'
import defaultConfig from '@/assets/js/app/config'
import cmlGlobal from '@/assets/js/app/cmlpanel/util/cmlGlobal'

const context = require.context('./anov/part', false, /\.js$/)

const modules = context.keys().reduce((module, filePath) => {
  const fileName = filePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = context(filePath)
  module[fileName] = value.default
  return module
}, {})
//数据源默认使用用户配置
anovStore.dataSource.state.type = defaultConfig.dataSource.type
// localStorage name-每次编译后清除之前存储
const lsn = `${cmlGlobal.projectLocalDir}_${cmlGlobal.projectId}_${
  cmlGlobal.platformVersion && cmlGlobal.platformVersion.replace(/\./g, '-')
}_${cmlGlobal.projectTime}`
const lsnNoTimeStamp = `${cmlGlobal.projectLocalDir}_${cmlGlobal.projectId}_${
  cmlGlobal.platformVersion && cmlGlobal.platformVersion.replace(/\./g, '-')
}`
const localKyes = Object.keys(localStorage)
for (let i = 0; i < localKyes.length; i++) {
  let item = localKyes[i]
  if (item.includes(lsnNoTimeStamp) && item !== lsn) {
    localStorage.removeItem(item)
    break
  }
}
//每次通过mutation修改state的时候console.log
// import createLogger from 'vuex/dist/logger'
Vue.use(Vuex)
//如果是npm run dev的时候就是dev环境
//如果npm run build时候就是production环境
//检测在开发环境下修改state是不是通过mutation
const debug = process.env.NODE_ENV !== 'production'

export default new Vuex.Store({
  modules: {
    ...anovStore,
    ...modules
  },
  actions,
  getters,
  state,
  mutations,
  strict: debug,
  plugins: [
    createVuexAlong({
      name: lsn,
      local: {
        list: [
          // AI
          // 'voiceFeedback.isStart',
          // 'voiceFeedback.synthesisType',
          // 'voiceRecognize.iatType',
          // 'faceRecognize.isStart',
          // 'gestureRecognize',
          // 'lightSensor.isStart',
          // theme
          'theme',
          // sound
          'soundServer',
          // i18n
          'language.lang',
          // license
          'license',
          // animate
          'animate',
          // data source
          'dataSource',
          // 大屏信息
          'bigScreenInfo',
          'env.customer.info',
          // 页面配置
          !debug ? 'page' : '',
          // 设计尺寸
          !debug ? 'env.designSize' : ''
        ],
        isFilter: false
      }
    })
  ]
})
